import type { IdentityRoleCreateDto, IdentityRoleDto, RoleCreateDto, RoleUpdateDto, UpdatePermissionDto } from "@/utils/api/role/type";
import { DataPermission } from "@/utils/api/type";

export interface RoleCreateView extends Omit<IdentityRoleCreateDto, 'extraProperties'> {
    permissions: Array<UpdatePermissionDto>;
    CustomDataPermission: string[];
    DataPermission: DataPermission;
}

export interface RoleUpdateView extends RoleCreateView {
    isStatic: boolean;
    concurrencyStamp?: string;
    id: string;
}

export const createRoleCreateView = (role: RoleCreateDto): RoleCreateView => {
    const roleCreateView = {
        name: role.name,
        isDefault: role.isDefault,
        isPublic: role.isPublic,
        permissions: role.permissions,
        CustomDataPermission: role.extraProperties?.CustomDataPermission.split(',') ?? [],
        DataPermission: role.extraProperties?.DataPermission ? Number(role.extraProperties?.DataPermission) as DataPermission : DataPermission.All,
    };
    return roleCreateView;
};

export const createRoleCreateDto = (role: RoleCreateView): RoleCreateDto => {
    const roleCreateDto = {
        name: role.name,
        isDefault: role.isDefault,
        isPublic: role.isPublic,
        permissions: role.permissions,
        extraProperties: {
            CustomDataPermission: role.CustomDataPermission.join(','),
            DataPermission: role.DataPermission,
        },
    };
    return roleCreateDto;
};


export const createRoleUpdateView = (role: IdentityRoleDto): RoleUpdateView => {
    const roleUpdateView = {
        id: role.id,
        concurrencyStamp: role.concurrencyStamp,
        name: role.name,
        isDefault: role.isDefault,
        isPublic: role.isPublic,
        isStatic: role.isStatic,
        permissions: [],
        CustomDataPermission: role.extraProperties?.CustomDataPermission.split(',') ?? [],
        DataPermission: role.extraProperties?.DataPermission ? Number(role.extraProperties?.DataPermission) as DataPermission : DataPermission.All,
    };
    return roleUpdateView;
};

export const createRoleUpdateDto = (role: RoleUpdateView): RoleUpdateDto => {
    const roleUpdateDto = {
        id: role.id,
        concurrencyStamp: role.concurrencyStamp,
        name: role.name,
        isDefault: role.isDefault,
        isPublic: role.isPublic,
        isStatic: role.isStatic,
        permissions: role.permissions,
        extraProperties: {
            CustomDataPermission: role.CustomDataPermission.join(','),
            DataPermission: role.DataPermission,
        },
    };
    return roleUpdateDto;
};

